package com.acxy.b2b2c.service;

import com.acxy.ants.common.enums.DataSourceType;
import com.acxy.ants.common.kit.FileKit;
import com.acxy.ants.common.kit.StrKit;
import com.acxy.ants.core.annotation.Service;
import com.acxy.ants.core.annotation.Source;
import com.acxy.ants.core.annotation.Tx;
import com.acxy.ants.core.exception.TipException;
import com.acxy.ants.core.plugin.db.Db;
import com.acxy.ants.core.plugin.entity.Symbol;
import com.acxy.ants.resful.bean.Page;
import com.acxy.b2b2c.entity.Goods;
import com.acxy.cms.entity.Article;
import com.acxy.cms.entity.Category;

import java.util.Date;

/**
 * @author MrShun
 * @version 1.0
 * @Date 2017-10-09
 */
@Service
public class GoodsService {

    @Source(DataSourceType.DRUID)
    private Db db;

    public Page queryPage(int index, int size, Long[] catIds) {
        Goods goods = new Goods().label("id, goods_sn, goods_name, goods_brief, be_mall, sort_order, goods_num, hits, goods_type, goods_thumb, is_show, " +
                "market_price, shop_price, promote_price, promote_start_date, promote_end_date, create_time, c.cat_name as catName").orderBy("sortOrder, createTime", Symbol.ASC);
        goods.leftJoin("left join category c on c.cat_id = _.cat_id");
        if (catIds != null && catIds.length > 0) {
            StringBuffer sb = new StringBuffer("_.cat_id in(");
            for (int i = 0; i < catIds.length; i++) {
                sb.append(catIds[i]);
                if (i != catIds.length - 1) sb.append(",");
            }
            sb.append(")");
            goods.where(sb.toString());
        }
        return goods.page(db, index, size);
    }

    /**
     * 添加商品
     * @param goods
     */
    public void save(Goods goods) {
        goods.setWarnNum(0);
        goods.setIsShow(1);
        goods.setCreateTime(new Date());
        goods.save(db);
    }

    public Goods find(Long id) {
        return new Goods(id).find(db);
    }

    public int update(Goods goods) {
        goods.setLastUpdate(new Date());
        return goods.update(db);
    }

    @Tx
    public int delete(Long[] ids, String rootPath) {
        if (ids == null) throw new TipException("ids 参数不能为空!");
        int res = 0;
        Goods goods = new Goods();
        for (Long id : ids) {
            Goods g = goods.find(db);
            //根据ID删除文章
            g.deleteById(db, id);
            //删除封面图片
            String thumb = g.getGoodsThumb();
            if (StrKit.notBlank(thumb)) FileKit.delete(rootPath + thumb);
            //删除多图片
            String imgs = g.getGoodsImgs();
            if (StrKit.notBlank(imgs)) {
                for(String img: imgs.split(","))
                    FileKit.delete(rootPath + img);
            }
            res++;
        }
        return res;
    }

    public int checkShow(Long id, int show) {
        Goods goods = new Goods(id);
        goods.setIsShow(show);
        return goods.update(db);
    }


    public int updateSort(Long id, Integer sort) {
        if (id == null) throw new TipException("id 参数不能为空!");
        Goods goods = new Goods(id);
        goods.setSortOrder(sort);
        return goods.update(db);
    }
}
